package com.yq.controller;

import com.yq.domain.SysMenu;
import com.yq.service.SysMenuService;
import com.yq.service.impl.SysMenuServiceImpl;
import com.yq.vo.MenuAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sys/menu")
public class MenuController {

    @Autowired
    private SysMenuServiceImpl sysMenuService;

    @GetMapping("nav")
    public ResponseEntity<MenuAuthVo> login(){
        //拿到登录信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getPrincipal().toString();
        List<SysMenu> menuList = sysMenuService.selectMenu(userId);
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        List<String> authoritiesList = authorities.stream().map(Object::toString)
                .collect(Collectors.toList());
        MenuAuthVo menuAuthVo = new MenuAuthVo();
        menuAuthVo.setMenuList(menuList);
        menuAuthVo.setAuthorities(authoritiesList);
        return ResponseEntity.ok(menuAuthVo);
    }

}
